<?php

namespace app\common\service;


use app\common\exception\CustomException;
use think\Db;

class HostModuleRelation extends BaseService
{

   /**
    * @name: 添加主机初始化
    * @Param: 
    * @Description: 
    * @Author: foo
    * @Date: 2019-12-10 17:11:39
    */
   public function init ($host_id)
   {
      return Db::name('HostModuleRelation')->insert(['host_id' => $host_id]);
   }

   /**
    * @name: 卸载客户端
    * @Param: 
    * @Description: 
    * @Author: foo
    * @Date: 2019-12-10 17:57:56
    */
   public function uninstall ($host_id_a)
   {
      Db::name('HostModuleRelation')->whereIn('host_id', $host_id_a)->delete();
   }
   /**
    * @name: 添加主机到模块
   * @Param: 
   * @Description: 
   * @Author: foo
   * @Date: 2019-12-02 15:15:28
   */
   public function add ($host_id_a, $module)
   {
      Db::name('HostModuleRelation')->whereIn('host_id', $host_id_a)->update([$module => 1]);
   }

   /**
    * @name: 删除模块中的 主机
    * @Param: 
    * @Description: 
    * @Author: foo
    * @Date: 2019-12-02 15:15:59
    */
   public function del ($host_id_a, $module)
   {
      Db::name('HostModuleRelation')->whereIn('host_id', $host_id_a)->update([$module => 0]);
   }

   /**
    * @name: 获取表字段
    * @Param: 
    * @Description: 
    * @Author: foo
    * @Date: 2019-12-02 15:18:39
    */
   public function getFields()
   {
      $field = Db::query('SHOW COLUMNS FROM ' . config('database.prefix') . 'host_module_relation' );
      if (empty($field))
      {
            $this->setError( $this->CODE_FAIL, '系统错误!!请联系技术员');
            return [];
      }

      $return = array_column($field, 'Field');

      if(count($return) <= 2)
      {
         $this->setError( $this->CODE_FAIL, '该系统未添加任意模块!!请联系管理员');
         return [];
      }
      
      return $return;
   }

   /**
    * @name: 模块检测
    * @Param: 
    * @Description: 
    * @Author: foo
    * @Date: 2019-12-02 15:25:51
    */
   public function fieldCheck($module)
   {

      $return = $this->getFields();

      if(!array_search($module, $return))
      {
         throw new CustomException('未找到该模块信息', 422);
      }

      return $module;
   }

   /**
    * @name: 剩余添加数量
    * @Param: 
    * @Description: 
    * @Author: foo
    * @Date: 2019-12-03 15:44:19
    */
   public function existNum ($module)
   {
      $return = Db::name('HostModuleRelation')->where($module, 1)->count();

      return $return;
   }

   /**
    * @name: 检测开启
    * @Param: 
    * @Description: 
    * @Author: foo
    * @Date: 2019-12-04 16:09:09
    */
   public function checkStatus ($column, $status)
   {
      $host_a = $this->getHost('id');
      $host_id_a = Db::name('HostModuleRelation')->where($column, $status)->whereIn('host_id', $host_a)->column('host_id');
      if(!empty($host_id_a))
      {
         $host_a = $this->getHost();
         foreach ($host_id_a as $v)
         {
            $hostStr_a[] = $host_a[$v]['name'] ;
         }
         $statusStr = $status ? '开启' : '关闭';
         throw new CustomException('主机 [' . implode(']，[', $hostStr_a) . '] 已' . $statusStr . '该模块',$this->CODE_FAIL);
      }
   }

   /**
    * @name:查找单个 依据主机id
    * @Param: 
    * @Description: 
    * @Author: foo
    * @Date: 2019-12-07 14:20:44
    */
   public function findByHostId ($host_id, $field = '*')
   {
      $return = Db::name('HostModuleRelation')->field($field)->where('host_id', $host_id)->find();

      return $return;
   }

    /**
     * @name 检测netfilter或者hgshield的状态
     * @author wx
     * @date 2020/1/7 18:25
     */
   public function findModuleStatus($host_id)
   {
        $info = Db::name('HostModuleRelation')->where('host_id',$host_id)->find();
        if($info['netfilter'] == 0 &&  $info['hgshield'] == 0){
            return false;
        }else{
            return true;
        }
   }
}